package com.husky.diyservice.common.aspect;

import com.husky.diyservice.common.annotation.LogsAnnotation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class LogsAop {

    // 声明切点，即定义拦截规则，确定有哪些方法会被切入
    @Pointcut("@annotation(com.husky.diyservice.common.annotation.LogsAnnotation)")
    public void pointcut() {
    }

    //环绕通知
    @Around("pointcut()")
    public Object testAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        LogsAnnotation annotation = signature.getMethod().getAnnotation(LogsAnnotation.class);
            if(annotation != null){
                Object[] args = proceedingJoinPoint.getArgs();
                log.info("方法={}，入参args={}",signature.getName(),args);
            }
       return proceedingJoinPoint.proceed();
    }

}
